<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Multimedia_Models_Mysql_Video extends Modules_Multimedia_Models_Video
{	
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'multimedia_video 
							WHERE video_id = ' . $this->_conn->escape($id) . ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}

	public function getLatestVideo($limit = 5)
	{
		$sql = 'SELECT v.* FROM ' . $this->_conn->_tablePrefix . 'multimedia_video AS v
				WHERE v.status = "active"';
		
		$sql .= ' ORDER BY v.created_date DESC';
		if ($limit) {
			$sql .= ' LIMIT ' . (int)$limit;
		}
		$rs = $this->_conn->query($sql);
		$videos = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$videos[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $videos;
	}
	
	public function increaseViews($articleId)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'multimedia_video 
					SET num_viewed = num_viewed + 1 
					WHERE video_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($articleId));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function add($video)
	{
		return $this->_conn->insert($video, $this->_conn->_tablePrefix . 'multimedia_video');	
	}
	
	public function find($condition = array(), $offset = null, $limit = null)
	{
		$sql = 'SELECT v.* FROM ' . $this->_conn->_tablePrefix . 'multimedia_video AS v
				WHERE 1';
		if (isset($condition['keyword']) && $condition['keyword']) {
			$sql .= ' AND v.title LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
		}
		if (isset($condition['video_id']) && $condition['video_id']) {
			$sql .= ' AND v.video_id = "' . $this->_conn->escape($condition['video_id']) . '"';
		}
		
		$sql .= ' ORDER BY v.created_date DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		$rs = $this->_conn->query($sql);
		$videos = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$videos[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $videos;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(DISTINCT v.video_id) as num_videos FROM ' . $this->_conn->_tablePrefix . 'multimedia_video AS v
				WHERE 1';
		if (isset($condition['keyword']) && $condition['keyword']) {
			$sql .= ' AND v.title LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
		}
		if (isset($condition['video_id']) && $condition['video_id']) {
			$sql .= ' AND v.video_id = "' . $this->_conn->escape($condition['video_id']) . '"';
		}
		
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_videos'];
	}
	
	public function update($video)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'multimedia_video 
					SET title = "%s", slug = "%s", description = "%s", status = "%s", 
						image_url = "%s", file_url = "%s"
					WHERE video_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($video['title']),
							$this->_conn->escape($video['slug']),
							$this->_conn->escape($video['description']),
							$this->_conn->escape($video['status']),
							$this->_conn->escape($video['image_url']),
							$this->_conn->escape($video['file_url']),
							$this->_conn->escape($video['video_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function delete($articleId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'multimedia_video 
							WHERE video_id = ' . $this->_conn->escape($articleId);
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function updateStatus($articleId, $status)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'multimedia_video 
					SET status = "%s" 
					WHERE video_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($status),
							$this->_conn->escape($articleId));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
}